/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.wsapi.module.user.service.impl;

import com.wsapi.commons.KnownException;
import com.wsapi.commons.vo.Page;
import com.wsapi.commons.vo.PageList;
import com.wsapi.core.dao.AccountDao;
import com.wsapi.module.user.dao.UserDao;
import com.wsapi.module.user.dao.module.User;
import com.wsapi.module.user.service.UserManagementService;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author yangyan <y.os@qq.com>
 */
@Service
public class UserManagementServiceImpl implements UserManagementService {

    @Resource
    private UserDao userDao;

    @Resource
    private AccountDao accountDao;

    @Override
    public PageList<User> findByPage(Page page) {
        return this.userDao.findByPage(page);
    }

    @Override
    public List<User> findAll() {
        return userDao.findAll();
    }

    @Override
    @Transactional
    public void addUser(User user) {
        //检查账号是否已经存在
        boolean exist = accountDao.isExist("accName", user.getAccount().getAccName());
        if (exist) {
            throw new KnownException("账号名称已经存在");
        }

        accountDao.save(user.getAccount());
        user.setAccId(user.getAccount().getAccId());
        userDao.save(user);
        userDao.flush();
    }

    @Override
    @Transactional
    public void removeUser(User user) {
        userDao.delete(user.getAccId());
    }

    @Override
    @Transactional
    public void updateUser(User user) {
        userDao.update(user);
    }

}
